package aom.ju.ss.view.user.shop.management

import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ManageVideoItemBinding
import aom.ju.ss.databinding.VideoListBinding
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.ManageShopVideoList
import aom.ju.ss.mode.ManageShopVideoListData
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.LP_API.shop_video_del_video
import aom.ju.ss.utils.http.LP_API.shop_video_video_list
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseActivity
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.base.TitleView
import aom.ju.ss.view.dialog.TitleDialog
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.google.gson.Gson


/**
 * 店铺 视频管理
 */
class VideoListActivity : BaseActivity() {
    private lateinit var binding: VideoListBinding
    private lateinit var pAdapter: PAdapter
    private var list = arrayListOf<ManageShopVideoListData>()
    override fun init() {
        binding = viewBinding as VideoListBinding
        binding.titleView15.r_title_two.text = "发布"
        binding.titleView15.r_title_two.setOnClickListener {
            startResult(PublishVideo::class.java, 1993)
        }

        pAdapter = PAdapter(
            list, R.layout.manage_video_item
        ) { bin, position ->
            val ib = bin as ManageVideoItemBinding
            val info = list[position]
            ib.info = info

            ib.rootView.setOnClickListener {
                list[position].ck = !list[position].ck
                pAdapter.notifyItemChanged(position)
                var all = true
                for (a in list) {
                    if (!a.ck) {
                        all = false
                        break
                    }
                }
                binding.all.text = if (!all) "   全选" else "   取消全选"
                binding.all.isChecked = all

            }
            val options = RequestOptions()
            Glide.with(activity!!)
                .setDefaultRequestOptions(options)
                .load(info.video_url)
                .into(ib.img)
        }
        binding.list.mRecycler.layoutManager = GridLayoutManager(activity, 3)
        binding.list.mRecycler.adapter = pAdapter
        binding.list.small.setOnRefreshListener {
            page = 1
            data
        }
        binding.list.small.setOnLoadMoreListener {
            page++
            data
        }
        binding.all.setOnClickListener {
            for ((a, i) in list.withIndex()) {
                list[a].ck = binding.all.isChecked
            }
            binding.all.text = if (binding.all.isChecked) "   全选" else "   取消全选"
            pAdapter.notifyDataSetChanged()
        }

        val ins = arrayListOf<String>()
        binding.del.setOnClickListener {
            ins.clear()
            for (a in list) {
                if (a.ck) {
                    ins.add(a.id.toString())
                }
            }
            if (ins.size == 0) {
                "请选择需要删除的视频".show
                return@setOnClickListener
            }

            TitleDialog().show(activity, object : TitleDialogInterface {
                override fun ok(view: View?, IsDetermine: Boolean) {
                    if (IsDetermine) {
                        OKHttpUtils(activity)
                            .SetApiUrl(shop_video_del_video)
                            .SetKey("id")
                            .SetValue(Gson().toJson(ins))
                            .POST(this@VideoListActivity)
                    }
                }

            }).tvDetermine("删除")
                .setConnect("确定删除?")


        }
        data
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK && requestCode == 1993) {
            page = 1
            data
        }
    }

    var page = 1
    val data: Unit
        get() {
            OKHttpUtils(activity)
                .SetApiUrl(shop_video_video_list)
                .SetKey("page")
                .SetValue(page.toString())
                .POST(this)
        }

    override fun ok(jsonString: String, httpTY: String) {
        super.ok(jsonString, httpTY)
        if (httpTY == shop_video_del_video) {
            page = 1
            data
            "删除成功".show
        } else if (httpTY == shop_video_video_list) {
            val mode = Gson().fromJson(jsonString, ManageShopVideoList::class.java)
            binding.list.small.Fin()
            if (page == 1) {
                list.clear()
            }
            mode.data?.let {
                list.addAll(it)
                pAdapter.notifyDataSetChanged()
            }
            binding.list.noData.VG(list.size == 0)
        }
    }

    override fun error(e: String, jsonString: String, httpTY: String) {
        super.error(e, jsonString, httpTY)
        e.show
        binding.list.small.Fin()
    }

    override val lay: Int = R.layout.video_list
}